/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.example.evaluation;

import org.nudt.jCloud.evaluation.Evaluation;
import org.nudt.jCloud.fileSystem.BalanceStorer;
import org.nudt.jCloud.fileSystem.FileSystem;
import org.nudt.jCloud.fileSystem.FileSystemGenerator;
import org.nudt.jCloud.infrastructure.HomegeneousCluster;
import org.nudt.jCloud.infrastructure.SlotType;
import org.nudt.jCloud.jobScheduler.AFCFSScheduler;
import org.nudt.jCloud.jobScheduler.JobScheduler;
import org.nudt.jCloud.jobScheduler.LocalityDelayScheduler;
import org.nudt.jCloud.jobScheduler.LocalityFairScheduler;
import org.nudt.jCloud.resourceManager.GeneralReservationRM;
import org.nudt.jCloud.resourceManager.ResManager;
import org.nudt.jCloud.resourceScaler.ConstantScaler;
import org.nudt.jCloud.resourceScaler.ResourceScaler;
import org.nudt.jCloud.simulation.CloudSchedSIM;
import org.nudt.jCloud.simulation.SIMTag;
import org.nudt.jCloud.simulation.User;
import org.nudt.jCloud.simulation.UserBroker;
import org.nudt.jCloud.workload.BatchWLfromFileGen;
import org.nudt.jCloud.workload.Workload;
import org.nudt.jCloud.workload.WorkloadGenerator;

public class Locality_Example {
	private static HomegeneousCluster homeCluster;
	private static ResManager resourceManager;
	private static UserBroker userBroker;
	private static JobScheduler jobScheduler;
	private static ResourceScaler resourceScaler;
	private static SlotType slotType = new SlotType(1, 1000, 250, 0);
	private static Workload wl;
	private static User user;
	
	private static FileSystem fileSystem;
	
	public static void init(String simName){
		CloudSchedSIM.init(simName);
		homeCluster = new HomegeneousCluster(60, 1000, 1, 4000, 1000, 200000);
		BalanceStorer storer = new BalanceStorer();
		fileSystem = new FileSystem(homeCluster, storer);
		FileSystemGenerator fileSystemGen = new FileSystemGenerator(1000, 500, 200, 10);
		fileSystemGen.generate(fileSystem, storer);
		WorkloadGenerator wlGen = new BatchWLfromFileGen(fileSystem, 50, 50, slotType);
		wl = wlGen.generate(0);
		
		resourceManager = new GeneralReservationRM("ReservationRM", homeCluster);
		userBroker = new UserBroker("UserBroker", SIMTag.RM_MODE_R);
		resourceScaler = new ConstantScaler(slotType, 60);
		jobScheduler = new AFCFSScheduler("AFCFS");
		userBroker.init(jobScheduler, resourceScaler);
		user = new User("user", wl);
		userBroker.addUser(user);
	}
	
	public static void reset(String simName, JobScheduler jobScheduler){
		CloudSchedSIM.reset(simName);
		resourceManager.reset();
		user.reset();
		userBroker.reset();
		resourceScaler.reset();
		userBroker.init(jobScheduler, resourceScaler);
		userBroker.addUser(user);
	}
	
	public static void simulate(){
		CloudSchedSIM.addRM(resourceManager);
		CloudSchedSIM.addUserBroker(userBroker);
		CloudSchedSIM.startSimulation();
	}
	
	public static void main(String[] args){		
		init("Locality_AFCFS_Example");
		simulate();	
		jobScheduler = new LocalityFairScheduler("LocalityFair");
		reset("Locality_Fair_Example", jobScheduler);
		simulate();
		jobScheduler = new LocalityDelayScheduler("LocalityDelay", 3);
		reset("Locality_Delay_Example", jobScheduler);
		simulate();
		
		Evaluation.wlSize(wl);
		System.out.println();
		Evaluation.avgWaitTime("Locality_AFCFS_Example", "user");
		Evaluation.avgWaitTime("Locality_Fair_Example", "user");
		Evaluation.avgWaitTime("Locality_Delay_Example", "user");
		System.out.println();
		Evaluation.localityCount("Locality_AFCFS_Example", "user");
		Evaluation.localityCount("Locality_Fair_Example", "user");
		Evaluation.localityCount("Locality_Delay_Example", "user");
		
	}

}
